/**
 * Created by yuzhou on 16/9/28.
 */
define([
    'vue', 'text!tpl/goToTop.html',
], function(Vue, goToTopHtml) {

    return Vue.extend({
        template: goToTopHtml,
        data: function(){
            return {
                show: false,
                timer: -1,
                scrollPos: 0
            }
        },
        methods: {
            go: function() {
                var vm = this
                clearInterval(vm.timer);
                vm.timer = setInterval(function(){
                    var speed = (0 - vm.scrollPos)/10;
                    speed = Math.floor(speed);
                    if(vm.scrollPos == 0){
                        clearInterval(vm.timer);
                    }
                    document.body.scrollTop = vm.scrollPos  + speed
                }, 30);
            }
        },
        ready: function(){
            var vm = this
            window.onscroll = function() {
                vm.scrollPos = document.body.scrollTop
                var wh = window.innerHeight
                if(vm.scrollPos >= wh/2) {
                    vm.show = true
                } else {
                    vm.show = false
                }
            }
        }
    })
})